/*  
	------------------------------------------------------------------------
	 NQ Core Library
	
	 Homepage: http://www.awzhome.de/
	------------------------------------------------------------------------
	
	This Source Code Form is subject to the terms of the Mozilla Public
	License, v. 2.0. If a copy of the MPL was not distributed with this
	file, You can obtain one at http://mozilla.org/MPL/2.0/.
   
	The Original Code is code of NQ Core Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2012 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	------------------------------------------------------------------------
*/

using System;
using System.Threading;
using AWZhome.NQ.Core;
using System.Windows;

namespace AWZhome.NQ.Host
{
	static class StartUp
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		public static void Main(string[] args)
		{
#if !DEBUG
			// Handler for uncatched exceptions
			NQExceptionHandler teh = new NQExceptionHandler();
			//Application.ThreadException += new ThreadExceptionEventHandler(teh.OnThreadException);
#endif

			// Initializing of NQ service manager
			NQServiceManagerImpl servman = new NQServiceManagerImpl(args, NQHostMode.GUI);
			NQServiceManager.SetImplementation(servman);

			// Add log event handler
			NQServiceManager.Instance.MessageLogged += ServiceManager_MessageLogged;

#if !DEBUG
			try
			{
#endif
				// Load all components in plugin directory
				servman.LoadComponents();
				servman.LoadServices();

#if !DEBUG
			}
			catch (Exception ex)
			{
				NQExceptionHandler.ShowExceptionMessage(ex);
			}
			finally
			{
#endif
				NQServiceManager.Instance.MessageLogged -= ServiceManager_MessageLogged;
				servman.DestroyServices();
#if !DEBUG
			}
#endif
		}

		static void ServiceManager_MessageLogged(object sender, NQLogEventArgs e)
		{
			// Output log messages on standard output
			string typetext = String.Empty;
			switch (e.LogType)
			{
				case NQLogType.Debug:
					typetext = "DEBUG";
					break;

				case NQLogType.Error:
					typetext = "ERROR";
					break;

				case NQLogType.Info:
					typetext = "INFO";
					break;
			}

			DateTime timestamp = DateTime.Now;
			Console.WriteLine("[{0:G}] {1}: {2}", timestamp, typetext, e.LogMessage);
		}
	}

#if !DEBUG
	public class NQExceptionHandler
	{

		public void OnThreadException(object sender, ThreadExceptionEventArgs e)
		{
			// Fehlermeldung anzeigen
			ShowExceptionMessage(e.Exception);
		}

		public static void ShowExceptionMessage(Exception catchedex)
		{
			try
			{
				MessageBox.Show(catchedex.ToString(), "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
			}
			catch (Exception)
			{
				try
				{
					MessageBox.Show("A fatal error has occured. The application will be closed.", "", MessageBoxButton.OK, MessageBoxImage.Stop);
				}
				finally
				{
					Application.Current.Shutdown();
				}
			}
		}
	}
#endif

}